/**
 * 
 */
package com.wodsy.phoenix.apps.shoppingcart.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wodsy.phoenix.dataobject.base.Message;
import com.wodsy.phoenix.dataobject.shoppingcart.model.ShoppingcartItem;
import com.wodsy.phoenix.dataservice.shoppingcart.service.ShoppingcartItemService;

/**
 * @author bryan
 *
 */
@Controller
@RequestMapping("/shoppingcart")
public class ShoppingcartController {
	private Logger logger = LoggerFactory.getLogger(ShoppingcartController.class);
	
	@Autowired
	//@Reference(interfaceClass=ShoppingcartItemService.class,interfaceName="com.wodsy.phoenix.dataservice.shoppingcart.service.ShoppingcartItemService")
	private ShoppingcartItemService shoppingcartItemService;

	/**
     * 导向首页面
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {
        return "/index";
    }
    
    /**
     * 产品详情中商品添加到购物车功能
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/cartAdd/{skuId}/{skuCount}", method = RequestMethod.GET)
    @ResponseBody
    public Message addGoodsToShoppingcart(@PathVariable int skuId,@PathVariable int skuCount,HttpServletRequest request) {
        
    	Integer userId = 1;//TODO 获取userId
    	ShoppingcartItem sci = new ShoppingcartItem();//TODO 获取sku信息(shopId)
    	Integer shopId = 1;//TODO 获取shopId
    	sci.setShopId(shopId);
    	sci.setSkuId(skuId);
    	sci.setSkuCount(skuCount);
    	shoppingcartItemService.addMyShoppingcartItem(userId, sci);
    	
    	return new Message();
    }
    
    /**
     * 购物车中删除商品
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/goodsDel/{skuId}", method = RequestMethod.GET)
    @ResponseBody
    public Message delGoodsFromShoppingcart(@PathVariable int skuId,HttpServletRequest request) {
        
    	Integer userId = 1;//TODO 获取userId
    	Integer shopId = 1;//TODO 获取shopId
    	
    	shoppingcartItemService.delShoppingcartItem(userId, shopId, skuId);
    	
    	return new Message();
    }
    
    /**
     * 购物车中更新商品数量
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/updateCartItem/{skuId}/{skuCount}", method = RequestMethod.GET)
    @ResponseBody
    public Message updateGoodsToShoppingcart(@PathVariable int skuId,@PathVariable int skuCount,HttpServletRequest request) {
        
    	Integer userId = 1;//TODO 获取userId
    	
    	ShoppingcartItem sci = new ShoppingcartItem();//TODO 获取sku信息
    	Integer shopId = 1;//TODO 获取shopId
    	sci.setShopId(shopId);
    	sci.setSkuId(skuId);
    	sci.setSkuCount(skuCount);

    	shoppingcartItemService.updateShoppingcartItem(userId, sci);
    	
    	return new Message();
    }
    
    /**
     * 获取购物车中商品信息
     *
     * @param request 请求
     * @return
     */
    @RequestMapping(value = "/shoppingcartlist", method = RequestMethod.GET)
    @ResponseBody
    public Message findGoodsFromShoppingcart(HttpServletRequest request) {
        Message msg = new Message();
    	Integer userId = 1;//TODO 获取userId

    	List<ShoppingcartItem> shoppingcartItemList = shoppingcartItemService.getMyShoppingcartItemListByUid(userId);
    	msg.setRs(shoppingcartItemList);
    	return msg;
    }
    
    
    
}
